<?php
/**
 * 用户上传接口
 */
require_once dirname(__DIR__)."/login_filter.php";
//require_once dirname(dirname(__DIR__))."/common.php";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\cls\sdk\FoundaoVideoSDK;
use app\cls\tom\FFmpeg;
use app\cls\tom\Config;
use app\model\UserVideo;

//话题参数
$join_sub_id=$_REQUEST['join_sub_id'];
$join_sub=$_REQUEST['join_sub'];
if(empty($join_sub_id) || empty($join_sub)){
    Response::output(-1,"请选择话题");
}
//视频和滤镜参数
$video_url=$_REQUEST['video_url'];//视频地址
$video_desc=$_REQUEST['video_desc'];//视频地址
if(empty($video_url)){
    Response::output(-1,"视频不能为空");
}
$fiflter=$_REQUEST['filter'];//滤镜类型
//音效参数
$audio_url=$_REQUEST['audio_url'];//音效地址
$audio_id=$_REQUEST['audio_id'];//音效id
//贴纸参数
$tiezhi=$_REQUEST['tiezhi'];//贴纸地址
$tiezhi_x=$_REQUEST['tiezhi_x'];//贴纸x坐标
$tiezhi_y=$_REQUEST['tiezhi_y'];//贴纸y坐标
$tiezhi_height=$_REQUEST['tiezhi_height'];//贴纸高度
$tiezhi_width=$_REQUEST['tiezhi_width'];//贴纸宽度



//1.用户只是单纯的上传视频
if($fiflter=="none" && empty($audio_url) && empty($tiezhi)){
    //获取视频相关信息
    $ffmpeg = new FFmpeg(false);
    $wh=$ffmpeg->getVideoDetail($video_url,4);
    if(empty($wh['width'])){
        Response::output(-1,"视频宽度无法获取，请重新上传");
    }
    if(empty($wh['height'])){
        Response::output(-1,"视频高度无法获取，请重新上传");
    }
    if(empty($wh['duration'])){
        Response::output(-1,"视频时长无法获取，请重新上传");
    }
    //截图
    $img_path=upload_dir("image");
//    var_dump($img_path['savehttp']);exit;
    $re=$ffmpeg->getCoverFromVideo1($video_url,$img_path['savepath']);
    if(!$re){
        Response::output(-1,"截图失败",$ffmpeg->getLastError());
    }
    $data['pic']=$img_path['savehttp'];
    $data['material_id']=Tools::UUID();
    $data['video_uuid']=Tools::UUID();
    $data['uuid']=$Auth['uuid'];
    $data['nick_name']=$Auth['nick_name'];
    $data['nick_pic']=$Auth['nick_pic'];
    if(!empty($video_desc)){
        $data['video_desc']=htmlspecialchars($video_desc);
    }

    $data['width2']=$wh['width'];
    $data['height2']=$wh['height'];
    $data['duration']=$wh['duration'];
    $data['join_type']=$join_sub_id;
    $data['join_type_sub']=$join_sub;
    $data['video_url']=$video_url;
    $data['mix_status']=1;
    $data['type']=2;
    $data['is_job_status']=2;
    $data['add_time']=date("Y-m-d H:i:s",time());
    $db = SysResource::getDB(true);
    $UserVideo=new UserVideo($db);
    $res=$UserVideo->add_material($data);
    if($res){
        Response::output(0,"添加成功1");
    }else{
        Response::output(-1,"添加失败");
    }


}else{
//2.用户选择了滤镜，贴纸，音效
    $FoundaoVideoSDK=new FoundaoVideoSDK();
    //获取视频宽，高，时长
    $ffmpeg = new FFmpeg(false);
    $wh=$ffmpeg->getVideoDetail($video_url,4);
//    Response::output(0,"11",$wh,$video_url);
    //设置视频参数
    $option1=[];
    $option2=[];
    $option3=[];
    //设置滤镜参数
    if(empty($fiflter)){
        $option1['filter']="none";
    }else{
        $option1['filter']=$fiflter;
    }
//    $option1['duration']=$wh['duration']*1000;
//    if(empty($option1['duration'])){
//        Response::output(-1,"请重新添加，视频时长无法获取",$_SERVER['SERVER_ADDR']);
//    }
    $FoundaoVideo = $FoundaoVideoSDK->createInputVideo($video_url,$option1);
    $Foundao['FoundaoVideo']=$FoundaoVideo;
    //设置音效参数
    if(!empty($audio_url)){
        if(empty($audio_id)){
            Response::output(-1,"缺少音效id");
        }
        //这里的都是毫秒
        $option2['duration']=$wh['duration']*1000;
        if(empty($option2['duration'])){
            Response::output(-1,"请重新添加，视频时长无法获取",$_SERVER['SERVER_ADDR']);
        }
//        $option2['duration']=30*1000;
        $FoundaoAudio = $FoundaoVideoSDK->createInputAudio($audio_url,$option2);
        $data['music']=$audio_url;
        $Foundao['FoundaoAudio']=$FoundaoAudio;
    }
    //设置贴纸参数
    if(!empty($tiezhi)){
        //这里的都是毫秒
        $FoundaoTiezhi = $FoundaoVideoSDK->createInputLayer("img",$tiezhi_x,$tiezhi_y,0,$wh['duration']*1000,['img_url'=>$tiezhi,"width"=>$tiezhi_width,"height"=>$tiezhi_height]);
        $Foundao['FoundaoTiezhi']=$FoundaoTiezhi;
    }
    //设置输出路径
    $path=upload_dir("video","mp4");
    $ret1 = $FoundaoVideoSDK->createOutput(trim($path['savename'],"/"), "local", "video");
    $Foundao['createOutput']=$ret1;
    if (empty($ret1)) {
        Response::output(-1,$FoundaoVideoSDK->getError());
    }
//    Response::output(0,$Foundao,$_SERVER['SERVER_ADDR']);
    //提交给彬总接口处理
    $job_id = $FoundaoVideoSDK->submitJob();
    if(empty($job_id)){
        Response::output(-1,"提交失败，请重新提交一次",$_SERVER['SERVER_ADDR']);
    }
    //添加数据到数据库
    $data['material_id']=$job_id;
    $data['video_uuid']=Tools::UUID();
    $data['uuid']=$Auth['uuid'];
    $data['nick_name']=$Auth['nick_name'];
    $data['nick_pic']=$Auth['nick_pic'];
    if(!empty($video_desc)){
        $data['video_desc']=htmlspecialchars($video_desc);
    }
    $data['join_type']=$join_sub_id;
    $data['join_type_sub']=$join_sub;
    $data['video_url']=$path['savehttp'];
    $data['type']=2;
    $data['is_dub']=1;
    $data['is_job_status']=1;
    $data['add_time']=date("Y-m-d H:i:s",time());
    $db = SysResource::getDB(true);
    $UserVideo=new UserVideo($db);
    $res=$UserVideo->add_material($data);
    if($res){
        $dd['job_id']=$job_id;
        $dd['move_name']=$path['movename'];
        $dd['video_url']=$path['savehttp'];
        Response::output(0,"添加成功2",$dd,$_SERVER['SERVER_ADDR']);
    }else{
        Response::output(-1,"添加失败");
    }
}








//获取毫秒级时间
function getMsecTime()
{
    list($msec, $sec) = explode(' ', microtime());
    $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    return $msectime;
}
//获取上传目录
function upload_dir($type="tmp",$suffix="jpg"){
    //解决跨域问题
    $allUploadConfig = Config::load("cos_upload");

    $uploadConfig = $allUploadConfig[$type];

    //创建上传目录，配置上传
    $date1=date("Y",time());
    $date2=date("m",time());
    $date3=date("d",time());
    $dir="/".$date1."/".$date2."/".$date3;
    $uploadpath1=$uploadConfig['savepath'];
    $uploadpath2=$uploadConfig['savehttp'];
    $key1=rand(1000,10000);
    $key2=rand(10000,20000);
    $key3=rand();
    $file_name=$key1.$key2.$key3;
    //这是服务器上的上传地址
    $uploadpath['savepath']=$uploadpath1.$dir."/".$file_name.".{$suffix}";
    $uploadpath['savename']=$dir."/".$file_name.".{$suffix}";
    $uploadpath['movename']=$uploadpath1.$dir;
    if($type=="video"){
        mkdir($uploadpath1.$dir."/".$file_name,0777,true);
    }
    //这是网络访问地址
    $uploadpath['savehttp']=$uploadpath2.$dir."/".$file_name.".{$suffix}";
    return $uploadpath;
}

